package cn.websocket.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.websocket.pojo.User;
import cn.websocket.service.UserService;
import cn.websocket.valid.common.ValidComment;
import cn.websocket.vo.Result;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import jakarta.validation.groups.Default;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import static cn.websocket.constant.StatusConstant.HTTP_SESSION_CURRENTUSER;
import static cn.websocket.constant.StatusConstant.USER_LOGOUT_SUCCESS;

/**
 * <h3>WebSocket</h3>
 * <p></p>
 *
 * @author gdw
 * @since 2025-08-18 10:05:43
 */
@RestController
@RequestMapping("/user")
@RequiredArgsConstructor
public class UserController {
    private final UserService userService;

    /**
     * 登录
     *
     * @Validated(ValidComment.Create.class)参数校验
     * @param user        提交的用户数据，包含用户名和密码
     * @param httpSession HttpSession
     * @return Result<Object>
     */
    @PostMapping("/login")
    public Result<Object> login(@Validated(ValidComment.Login.class) @RequestBody User user, HttpSession httpSession) {
        return userService.login(user,httpSession);
    }

    /**
     * 登出
     * @param
     * @return
     */
    @GetMapping("/logout")
    public Result<Object> logout( ) {
        StpUtil.logout();
        return Result.success(USER_LOGOUT_SUCCESS);
    }

    /**
     *
     * 用户注册
     * @param user     提交的用户数据
     * @return
     */
    @PostMapping("/register")
    public Result<Object> register(HttpServletRequest request, @Validated({ValidComment.Create.class}) @RequestBody User user) {
        return userService.register(request,user);
    }

    /**
     * 获取用户名
     *
     * @param httpSession HttpSession
     * @return String
     */
    @GetMapping("/getUsername")
    public Result<User> getUsername(HttpSession httpSession) {
        return userService.getUser(httpSession);
    }

}
